package com.lv.suyiyong.hfrbusines.service

import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import com.blankj.utilcode.util.LogUtils

class GetADService : Service() {

    //定义并初始化binder成员变量
    private val mBinder = DownloadBinder()
    override fun onBind(intent: Intent): IBinder {
        LogUtils.e("MyService", "onBind")
        return mBinder
    }

    override fun onCreate() {
        super.onCreate()
        LogUtils.e("MyService", "onCreate")
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        LogUtils.e("MyService", "onStartCommand")
        return super.onStartCommand(intent, flags, startId)
        //执行到某个条件，可以调用这个方法自动停止Service
        getAd();

        //停止服务的方法
//       stopSelf()
    }

    private fun getAd() {
        TODO("Not yet implemented")
    }

    override fun onDestroy() {
        super.onDestroy()
        LogUtils.e("MyService", "onDestroy")
    }

    //定义binder内部类
    class DownloadBinder : Binder() {
        private var i: Int = 0
        fun startDownload() {
            LogUtils.e("MyService", "startDownload")
            i++
        }

        fun getProgress(): Int {
            LogUtils.e("MyService", "getProgress = " + i)
            return i
        }
    }

    companion object {
        fun startService(mContext: Context) {
            val service = Intent(mContext, GetADService::class.java)
            mContext.startService(service)
        }
    }
}